<?php
$this->title='明源云客-官网管理-新闻管理';
?>
    <div class="section-table mt20">    
        <div class="table-header lh30 pb5 clearfix">
            <span class="fl f14">文章列表</span>
            <div class="fr btn-group">
                <button class="btn" id="add-btn">新建</button>
                <button class="btn btn-yellow" id="release-btn">发布</button>
                <button class="btn btn-yellow" id="cancel-release-btn">取消发布</button>
                <button class="btn" id="delete-btn">删除</button>
                <input type="text" placeholder="标题/关键字" class="fl input-text w200 ml3 ng-pristine ng-untouched ng-valid" id="search-keyword">
                <button class="btn" id="search-btn">查询</button>
            </div>
        </div>
        <div class="mt10">
            <table  id="grid-table" width="100%" border="0" cellspacing="0" cellpadding="0" style="border:0px #DFDFDF solid"></table>
            <div id="grid-pager"></div>
        </div>
    </div>

</div></div>
<script>
$(document).ready(function() {
	$('#search-btn').bind('click', function() {
        var keyword = $('#search-keyword').val();

        $(this).attr('disabled', 'disabled');
        $('#grid-table').setGridParam(
            {
                postData: {
                    keyword: keyword
                }
            }
        );
        $('#grid-table').trigger('reloadGrid');
        $('#search-keyword').focus();
    });

    $('#search-keyword').bind('keypress', function(e) {
        if(e.keyCode == 13)
        {
            $('#search-btn').trigger('click');
            return false;
        }
        else if(e.keyCode == 27)
        {
            $('#clean-keyword-btn').trigger('click');
            $(this).val('').blur().focus();
            return false;
        }
    }).focus();
    
    $('#add-btn').bind('click', function(){window.location = "?r=/official/article/edit"});
    
    $('#release-btn').bind('click', function(){
        $(this).attr('disabled', 'disabled');
        setBatch('status', 1);
        $('#grid-table').trigger('reloadGrid');
    });
    
    $('#cancel-release-btn').bind('click', function(){
        $(this).attr('disabled', 'disabled');
        setBatch('status', 0);
        $('#grid-table').trigger('reloadGrid');
    });
    
    $('#delete-btn').bind('click', function(){
        $(this).attr('disabled', 'disabled');
        setBatch('del', 1);
        $('#grid-table').trigger('reloadGrid');
    });    
    
    function setBatch (section, val) {
        var list = getList();
        $('#grid-table').setGridParam({
            postData: {
                ids: list,
                update: section,
                value: val
            }
        });          
    }

    function getList () {
        var rowData = jQuery('#grid-table').jqGrid('getGridParam','selarrrow');
        var list = "";
        if(rowData.length) {
            for(var i=0;i<rowData.length;i++){
               //var name= jQuery('#grid-table').jqGrid('getCell',rowData[i],'title');//name是colModel中的一属性
               list += ':'+ rowData[i]; 
            }
        }
        return list;
    }

    $('#grid-table').jqGrid(
        {
            colNames: [
                '#编号',
                '标题',
                '关键字',
                '分享次数',
                '发布状态',
                '分类',
                '排序',
                '修改时间',
                '作者',
                //'操作'
			],
            colModel: [
                { index: 'id', name: 'aId', align: 'center', width: 20 },
                { index: 'title', name: 'title', align: 'center', width: 70,editable:true },
                { index: 'tag', name: 'tag', align: 'center', width: 50,editable:true },
                { index: 'share_num', name: 'share_num', align: 'center', width: 30 },
                { index: 'status', name: 'status', align: 'center', width: 30 ,classes:'status', editable:true, edittype:'select', editoptions:{value:"0:未发布;1:已发布"}},
                { index: 'dirName', name: 'dirName', align: 'center', width: 30 },
                { index: 'sort', name: 'sort', align: 'center', width: 20, editable:true },
                { index: 'modifiedTime', name: 'modifiedTime', align: 'center', width: 50 },
                { index: 'createdBy', name: 'createdBy', align: 'center', width: 30 },
                //{ index: 'edit', name: 'edit', align: 'center', width: 90 }
            ],
            datatype: 'json',
            height: 'auto',
            autowidth: true,
			rowNum: 20,
            rowList: [1,10,20,50,100],
            viewrecords: true,
            viewsortcols: 'vertical',
			hoverrows: true,

            pager: $('#grid-pager'),
            sortname: 'id',
            sortorder: 'asc',
			multiselect: true,
            cellEdit: true,
            cellurl: '?r=/official/article/quick-set',
			afterSubmitCell: function(serverresponse, rowid, cellname, value, iRow, iCol) {
				if(''!=serverresponse.responseText){
					var response = $.parseJSON(serverresponse.responseText); 
                    return [response.status,response.content];
				}else return [true];
			},
            loadComplete: function() {
                $('#search-btn').attr('disabled', false);
                $('#release-btn').attr('disabled', false);
                $('#cancel-release-btn').attr('disabled', false);
                $('#delete-btn').attr('disabled', false);
            },
            ondblClickRow: function(id) {
                window.location.href = '?r=/official/article/edit&id=' + id;
            },
            onSelectRow : function(id) {

            }
        }
    ).navGrid(
        '#grid-pager',
        // options
        {
            search: false,
            addfunc: function() {
                window.location.href = '?r=/official/article/edit';
            },
            editfunc: function(id) {
                window.location.href = '?r=/official/article/edit&id=' + id;
            }
            
        },
        // edit
        {
        },
        // add
        {
        },
        // del
        {
            url: '?r=/official/article/delete'
        }
    );
});
function updateStatus(id, type, obj, fieldType) {
    url = {1:'/api/index.php?r=/official/article/set-top', 2:'/api/index.php?r=/official/article/release'};
    $.post(url[fieldType], {'id':id, 'type':type},function(data){
        if (type == 1) {
            if (data.success == 1) {
                fieldType == 2 && obj.parent().parent().find('.status').text('已发布');
                fieldType == 1 && obj.parent().parent().find('.betop').text('是');
                alert('设置成功！');
            } else {
                alert('设置失败！');
            }
        } else {
            if (data.success == 1) {
                fieldType == 2 && obj.parent().parent().find('.status').text('未发布');
                fieldType == 1 && obj.parent().parent().find('.betop').text('否');
                alert('设置成功！');
            } else {
                alert('设置失败！');
            }            
        }
    },'json');
}
</script>